package com.victor.controller.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.JingqubusinessOrder;
import com.victor.entity.JingqubusinessShop;
import com.victor.service.JbshopService;
import com.victor.service.OrderService;
import com.victor.utils.Msg;
/**
 * 电子商务
 * @author 
 *
 */
@RequestMapping("/order")
@RestController
public class OrderAppController {
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private JbshopService shopService;
	
	/**
	 * app端添加订单(TODO)
	 * @param order
	 * @return
	 */
	@RequestMapping("/addOrder")
	public Msg addOrder(@RequestBody JingqubusinessOrder order) {
		if (order==null) {
			return Msg.fail("参数错误");
		}
		//添加订单
		boolean b = orderService.addOrder(order);
		for (int i = 0; i < order.getJingqubusinessOrderShops().size(); i++) {
			order.getJingqubusinessOrderShops().get(i).setIoid(order.getId()+"");
			//添加订单中多个商品
			boolean as = orderService.addOrderShop(order.getJingqubusinessOrderShops().get(i));
			if (!as) {
				return Msg.fail("添加购买的商品列表失败");
			}
			for (int j = 0; j < order.getJingqubusinessOrderShops().get(i).getOrderShopParameters().size(); j++) {
				//添加每个商品的规格和参数
			}
		}
		return Msg.judge(b);
	}
	
	/**
	 * 订单列表
	 * @param page
	 * @param row
	 * @param uid
	 * @return
	 */
	@RequestMapping("/orderlist")
	public Msg findAll(int page,int row,String uid,String state) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		PageInfo<JingqubusinessOrder> selAllByUid = orderService.selAllByUid(uid,state,page,row);
		return Msg.success().add("list", selAllByUid);
	}
	/**
	 * 删除订单
	 * @param id
	 * @return
	 */
	@RequestMapping("/delorder")
	public Msg delOrder(int[] id) {
		if (id == null) {
			return Msg.fail("入参错误");
		}
		boolean b = orderService.batchDelete(id);
		boolean batchDeleteExpress = orderService.batchDeleteExpress(id);
		boolean batchDeleteShops = orderService.batchDeleteShops(id);
		if (!b || !batchDeleteExpress || !batchDeleteShops ) {
			return Msg.fail("删除订单失败");
		}
		return Msg.success();
	}
	/**
	 * 确认订单
	 * @param oid
	 * @return
	 */
	@RequestMapping("/sureorder")
	public Msg sureOrders(String oid) {
		if (oid == null || oid.isEmpty()) {
			return Msg.fail("入参失败");
		}
		JingqubusinessOrder selOrdersById = orderService.selOrdersById(oid);
		String[] shopclassid= new String[selOrdersById.getJingqubusinessOrderShops().size()];
		for (int i = 0; i < selOrdersById.getJingqubusinessOrderShops().size(); i++) {
			String classid = selOrdersById.getJingqubusinessOrderShops().get(i).getJingqubusinessShop().getShopclassid();
			shopclassid[i]=classid;
		}
		List<JingqubusinessShop> selAllShop = shopService.selAllShop(shopclassid);
		return Msg.success().add("order", selOrdersById).add("shop", selAllShop);
		
	}
	/**
	 * 确认收获
	 * @param state
	 * @return
	 */
	@RequestMapping("/suregoods")
	public Msg getGoogs(JingqubusinessOrder order) {
		if (order.getId() == null|| order.getState() == null || order.getState().isEmpty() || !order.getState().equals("3")) {
			return Msg.fail("入参错误");
		}
		boolean b = orderService.updateState(order);
		return Msg.judge(b);
	}

}
